package tests;
/*
 * Daniel Anderson
 * 
 * TCSS 360
 * Manuscript JUnit Tests
 * November 17, 2011
 * 
 */

import java.util.ArrayList;
import org.junit.Before;
import org.junit.Test;
import main.Manuscript;
import main.Review;
import main.Status;
import junit.framework.TestCase;

/**
 * JUnit tests for Manuscript
 * 
 * Cris added tests
 * @author Daniel Anderson
 * @version Dec 2
 */
public class ManuscriptTest extends TestCase {
	
	Manuscript manuscript1;
	Review review1;
	Review review2;
	Review reviewodd;
	ArrayList<Review> emptylist1;
	
	/**
	 * Initializes objects used in tests.
	 */
	@Before
	public void setUp() {
		manuscript1 = new Manuscript();
		review1 = new Review();
		review2 = new Review();
		reviewodd = new Review("odd", "Dan", "234", "Test");
		emptylist1 = new ArrayList<Review>();
	}
	
	/**
	 * Tests the initialization of a manuscript object.
	 */
	@Test
	public void testInit() {
		assertEquals("Manuscript status initially UNDECIDED?",
				Status.UNDECIDED , manuscript1.getStatus());
		
		assertEquals("Manuscript title initially Default_Title?", 
				"Default_Title" , manuscript1.getTitle());
		
		assertEquals("Manuscript author initially Default_Author?", 
				"Default_Author" , manuscript1.getAuthor());
		
		assertEquals("Manuscript ID initially -1?", "-1" , manuscript1.getAuthorID());
		
		assertEquals("Manuscript file initially Default?", 
				"default" , 
				manuscript1.getText());
		
		assertEquals("Manuscript reviews initially 0?", 0 , manuscript1.getNumReviews());
		
		assertEquals("Manuscript reviews list initially empty?",  
				emptylist1, manuscript1.getReviews());
	}//end testInit
	
	/**
	 * Tests Manuscript mutations for proper functionality.
	 * Holly revised 11/18 to reflect status type change to Status Enum
	 * Cris added setText and addReviewer test 12/2
	 */
	@Test
	public void testMutation() {
		//setStatus
		assertFalse("Manuscript status initially not APPROVED?", 
				Status.APPROVED == manuscript1.getStatus());
		manuscript1.setStatus(Status.APPROVED);
		assertEquals("Manuscript status changed to 1?", Status.APPROVED , 
				manuscript1.getStatus());
		
		//AddReview
		assertTrue("Manuscript initially has no reviews?", 0 == manuscript1.getNumReviews());
		manuscript1.addReview(review1);
		manuscript1.addReview(review2);
		assertEquals("Manuscript now has two reviews?", 2, manuscript1.getNumReviews());
		
		//AddReviewers
		assertEquals("Manuscript initially has no reviewers?", 0,
				manuscript1.getReviewers().size());
		manuscript1.addReviewer("reviewer1");
		manuscript1.addReviewer("reviewer2");
		assertEquals("Manuscript now has two reviewers?", 2, 
				manuscript1.getReviewers().size());
		
		//UpdateReview 
		int indexof1 = manuscript1.getReviews().indexOf(review1);
		int indexof2 = manuscript1.getReviews().indexOf(review2);
		assertTrue("review1 is inside manuscript1?", 
				manuscript1.getReviews().get(indexof1) == review1);
		assertTrue("review2 is inside manuscript1?",
				manuscript1.getReviews().get(indexof2) == review2);
		manuscript1.updateReview(review2, reviewodd);//replace review2 with reviewodd
		assertTrue("review1 is still inside manuscript1?", 
				manuscript1.getReviews().get(indexof1) == review1);
		assertTrue("review2 is no longer inside manuscript1?",
				manuscript1.getReviews().get(indexof2) != review2);
		assertTrue("review2 is now equal to reviewodd?",
				manuscript1.getReviews().get(indexof2) == reviewodd);
		
		//RemoveReview
		manuscript1.removeReview(reviewodd);
		assertFalse("manuscript1 does not contain reviewodd?", 
				manuscript1.getReviews().contains(reviewodd));
		
		//setText
		assertEquals("Manuscript text initially not default?", 
				"default", manuscript1.getText());
		manuscript1.setText("Not default");
		assertEquals("Manuscript text changed to Not default?", "Not default" , 
				manuscript1.getText());
	}//end testMutation
	
	/**
	 * Tests the behavior of the equals method
	 */
	@Test
	public void testEquals() {
		Manuscript man0 = new Manuscript("Title0", "Name", 
				"id0", "text0");
		Manuscript man1 = new Manuscript("Title0", "Name", 
				"id0", "text1");
		Manuscript man2 = new Manuscript("Title0", "Name", 
				"id1", "text0");
		Manuscript man3 = new Manuscript("Title1", "Name", 
				"id0", "text0");
		
		assertTrue("different text should still be equal", man0.equals(man1));
		assertFalse("different author id should not be equal", man0.equals(man2));
		assertFalse("different manuscript title should not be equal", man0.equals(man3));
	
	}//end testEquals
}//end of ManuscriptTest
